package cn.webrx.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-06-08 18:27
 */
public class MyRealm extends AuthorizingRealm {
    /**
     * 授权
     */
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
        return null;
    }

    /**
     * 认证
     */
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken at) throws AuthenticationException {
        String user = (String) at.getPrincipal();
        //String passwd = new String(((UsernamePasswordToken) at).getPassword());
        //这是散列后的值 md5 10 wx    991024dc17f3b5ef7e845ac8e5cdd612
        String password = "991024dc17f3b5ef7e845ac8e5cdd612"; //散列密文 ，应该是保存到数据中
        //从数据库获取盐
        String salt = "wx";
        //throw new CredentialsException("密码错误");
        //new LockedAccountException() 锁定
        //new UnknownAccountException() 未知
        //new DisabledAccountException()  禁用
        SimpleAuthenticationInfo sai = new SimpleAuthenticationInfo(at.getPrincipal(), password, ByteSource.Util.bytes(salt), this.getName());
        return sai;
        //return null;
    }
}
